const getBrowserInfo = () => {
  const _broswer: {
    version?: number;
    name?: string;
  } = {};

  const sUserAgent = navigator.userAgent;
  const isOpera = sUserAgent.indexOf("Opera") > -1;
  if (isOpera) {
    //首先检测Opera是否进行了伪装
    if (navigator.appName == 'Opera') {
      //如果没有进行伪装，则直接后去版本号
      _broswer.version = parseFloat(navigator.appVersion);
    } else {
      var reOperaVersion = new RegExp("Opera (\\d+.\\d+)");
      //使用正则表达式的test方法测试并将版本号保存在RegExp.$1中
      reOperaVersion.test(sUserAgent);
      _broswer.version = parseFloat(RegExp['$1']);
    }
    _broswer.name = 'opera';
  }
  const isChrome = sUserAgent.indexOf("Chrome") > -1;
  if (isChrome) {
    var reChorme = new RegExp("Chrome/(\\d+\\.\\d+(?:\\.\\d+\\.\\d+))?");
    reChorme.test(sUserAgent);
    _broswer.version = parseFloat(RegExp['$1']);
    _broswer.name = 'chrome';
  }
  //排除Chrome信息，因为在Chrome的user-agent字符串中会出现Konqueror/Safari的关键字
  const isKHTML = (sUserAgent.indexOf("KHTML") > -1
    || sUserAgent.indexOf("Konqueror") > -1 || sUserAgent
      .indexOf("AppleWebKit") > -1)
    && !isChrome;
  if (isKHTML) {//判断是否基于KHTML，如果时的话在继续判断属于何种KHTML浏览器
    const isSafari = sUserAgent.indexOf("AppleWebKit") > -1;
    const isKonq = sUserAgent.indexOf("Konqueror") > -1;
    if (isSafari) {
      const reAppleWebKit = new RegExp("Version/(\\d+(?:\\.\\d*)?)");
      reAppleWebKit.test(sUserAgent);
      // const fAppleWebKitVersion = parseFloat(RegExp["$1"]);
      _broswer.version = parseFloat(RegExp['$1']);
      _broswer.name = 'safari';
    } else if (isKonq) {
      // const reKong = new RegExp("Konqueror/(\\d+(?:\\.\\d+(?\\.\\d)?)?)");
      const reKong = new RegExp("Konqueror/(\\d+(?:\\.\\d+(\\.\\d)?))");
      reKong.test(sUserAgent);
      _broswer.version = parseFloat(RegExp['$1']);
      _broswer.name = 'konqueror';
    }
  }
  // !isOpera 避免是由Opera伪装成的IE
  const isIE = sUserAgent.indexOf("compatible") > -1
    && sUserAgent.indexOf("MSIE") > -1 && !isOpera;
  if (isIE) {
    const reIE = new RegExp("MSIE (\\d+\\.\\d+);");
    reIE.test(sUserAgent);
    _broswer.version = parseFloat(RegExp['$1']);
    _broswer.name = 'msie';
  }
  // 排除Chrome 及 Konqueror/Safari 的伪装
  const isMoz = sUserAgent.indexOf("Gecko") > -1 && !isChrome && !isKHTML;
  if (isMoz) {
    const reMoz = new RegExp("rv:(\\d+\\.\\d+(?:\\.\\d+)?)");
    reMoz.test(sUserAgent);
    _broswer.version = parseFloat(RegExp['$1']);
    _broswer.name = 'mozilla';
  }

  return _broswer;
}

export type BrowsersType = { name: string, minVersion: number }[]

export const isAllowBrowser = (browsers: BrowsersType) => {
  const result = {
    isAllow: true,
    browserName: "",
    browserVersion: "",
  };

  const browserInfo = getBrowserInfo();
  if (Object.keys(browserInfo).length > 0 && browsers && browsers.length > 0) {
    const { name, version } = browserInfo;
    for (let i = 0; i < browsers.length; i++) {
      result.browserName = name;
      result.browserVersion = version.toString();
      if (browsers[i].name == name && version >= browsers[i].minVersion) {
        result.isAllow = true;
        break;
      } else {
        result.isAllow = false;
      }
    }
  }

  return result;
}
